<template>
  <el-container v-loading="loading" class="post-article">
    <el-header class="header">
      <el-select
        v-model="article.cid"
        placeholder="请选择文章栏目"
        style="width: 150px;"
      >
        <el-option
          v-for="item in categories"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-input
        v-model="article.title"
        placeholder="请输入标题..."
        style="width: 400px;margin-left: 10px"
      ></el-input>
      <el-select
        v-model="article.tid"
        multiple
        :multiple-limit="3"
        placeholder="请选择文章标签"
        style="width: 300px;"
      >
        <el-option
          v-for="item in tags"
          :key="item.id"
          :label="item.tagName"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-header>
    <el-main class="main">
      <div id="editor">
        <mavon-editor
          style="height: 100%;width: 100%;"
          ref="md"
          @imgAdd="imgAdd"
          v-model="article.mdContent"
        ></mavon-editor>
      </div>
      <div
        style="display: flex;align-items: center;margin-top: 15px;justify-content: flex-end"
      >
        <el-button
          @click="cancelEdit"
          v-if="article.id != undefined && Number(article.id) > -1"
          >放弃修改</el-button
        >
        <template
          v-if="
            article.id == undefined || article.id == '' || article.id == '-1'
          "
        >
          <el-button @click="saveBlog(0)">保存到草稿箱</el-button>
          <el-button type="primary" @click="saveBlog(1)">发表文章</el-button>
        </template>
        <template v-else>
          <el-button type="primary" @click="saveBlog(1)">保存修改</el-button>
        </template>
      </div>
    </el-main>
  </el-container>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import { post, get, put, del, uploadFile } from "../utils/api";
import { isNotNullORBlank } from "../utils/utils";

export default {
  data() {
    return {
      categories: [],
      tags: [],
      tagInputVisible: false,
      tagValue: "",
      loading: false,
      from: "",
      article: {
        id: "-1",
        title: "",
        mdContent: "",
        cid: "",
        tid: []
      }
    };
  },
  watch: {
    "article.tid": function(val, oldVal) {
      console.log(val, oldVal);
    }
  },
  mounted: function() {
    this.getCategories();
    this.getTags();
    let id = this.$route.query.aid;
    if (id != null && id != "" && id != undefined) {
      this.article.id = id;
      this.loading = true;
      get("/article/" + this.article.id).then(resp => {
        this.loading = false;
        if (resp.code == 0) {
          this.article = resp.data;
          var tags = resp.data.tags;
          this.article.tid = [];
          for (var i = 0; i < tags.length; i++) {
            this.article.tid.push(tags[i].id);
          }
        } else {
          this.$message({ type: "error", message: "页面加载失败!" });
        }
      });
    }
  },
  methods: {
    cancelEdit() {
      this.$router.go(-1);
    },
    saveBlog(state) {
      if (
        !isNotNullORBlank(
          this.article.title,
          this.article.mdContent,
          this.article.cid
        )
      ) {
        this.$message({ type: "error", message: "数据不能为空!" });
        return;
      }

      this.loading = true;
      let params = {
        id: this.article.id,
        title: this.article.title,
        mdContent: this.article.mdContent,
        htmlContent: this.$refs.md.d_render,
        cid: this.article.cid,
        status: state,
        tid: this.article.tid
      };
      post("/article/", params).then(resp => {
        this.loading = false;
        if (resp.code == 0) {
          this.article.id = resp.data.msg;
          this.$message({
            type: "success",
            message: state == 0 ? "保存成功!" : "发布成功!"
          });

          if (state == 1) {
            this.$router.replace({ path: "/articleList" });
          }
        }
      });
    },
    imgAdd(pos, $file) {
      // 第一步.将图片上传到服务器.
      var formdata = new FormData();
      formdata.append("image", $file);
      uploadFile("/article/upload", formdata).then(resp => {
        if (resp.code == 0) {
          this.$refs.md.$imglst2Url([[pos, resp.data]]);
        } else {
          this.$message({ type: "error", message: resp.data });
        }
      });
    },
    getCategories() {
      get("/admin/category/").then(resp => {
        this.categories = resp.data;
      });
    },
    getTags() {
      get("/admin/tags/").then(resp => {
        this.tags = resp.data;
      });
    },
    handleClose(tag) {
      this.article.dynamicTags.splice(this.article.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.tagInputVisible = true;
      // eslint-disable-next-line no-unused-vars
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let tagValue = this.tagValue;
      if (tagValue) {
        this.article.dynamicTags.push(tagValue);
      }
      this.tagInputVisible = false;
      this.tagValue = "";
    }
  }
};
</script>
<style>
.post-article > .main > #editor {
  width: 100%;
  height: 450px;
  text-align: left;
}

.post-article > .header {
  background-color: #ececec;
  margin-top: 10px;
  padding-left: 5px;
  display: flex;
  justify-content: flex-start;
}

.post-article > .main {
  /*justify-content: flex-start;*/
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  background-color: #ececec;
  padding-top: 0px;
}

.post-article > .header > .el-tag + .el-tag {
  margin-left: 10px;
}

.post-article > .header > .button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.post-article > .header > .input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
